package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.Video;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Power(name = "视频管理",type =0)
@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    /**
     * 去资源审核页面
     *
     * @return
     */
    @Power(name = "去资源审核页面",type =1,url = "/video/queryvideo")
    @GetMapping(value = "queryvideo")
    public String queryvideo() throws Exception {
        return "video/queryvideo";
    }


    /**
     * 查询所有
     *
     * @return
     */
    @Power(name = "查询所有",type =1,url = "/video/queryvideo_result")
    @RequestMapping("/queryvideo_result")
    @ResponseBody
    public DataGridResultInfo querybusiness_result(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows, Video video) throws Exception {

        return videoService.page(page, rows, video);

    }

    /**
     * 去添加界面
     *
     * @return
     */
    @Power(name = "去添加界面",type =1,url = "/video/toaddvideo")
    @GetMapping("/toaddvideo")
    public String toaddvideo() {

        return "video/addvideo";
    }

    @Power(name = "添加",type =1,url = "/video/addsubmit")
    @PostMapping("/addsubmit")
    @ResponseBody
    public SubmitResultInfo addsubmit(String editorValue, Video video, @RequestParam(value = "myfile", required = false) CommonsMultipartFile file, HttpServletRequest request) throws Exception {
        video.setContent(editorValue);
        return videoService.addsubmit(file, video, request);
    }

    /**
     * 去修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @Power(name = "去修改页面",type =1,url = "/video/toupdate")
    @GetMapping("/toupdate")
    public String toupdate(String id, Model model) throws Exception {
        model.addAttribute("video", videoService.findById(id));
        return "video/updatevideo";
    }
    @Power(name = "修改",type =1,url = "/video/updatesubmit")
    @PostMapping("/updatesubmit")
    @ResponseBody
    public SubmitResultInfo updatesubmit(String editorValue, Video video, @RequestParam(value = "myfile", required = false) CommonsMultipartFile file, HttpServletRequest request) throws Exception {
        video.setContent(editorValue);
        return videoService.updatesubmit(file, video, request);
    }
    @Power(name = "删除",type =1,url = "/video/delete")
    @PostMapping("/delete")
    @ResponseBody
    public SubmitResultInfo delete(String id, HttpServletRequest request) throws Exception {
        return videoService.delete(id, request);
    }

    /**
     * 查看
     * @param id
     * @return
     * @throws Exception
     */
    @Power(name = "查看",type =1,url = "/video/findContext")
    @GetMapping("/findContext")
    @ResponseBody
    public Map findContext(String id) throws Exception {
        Map map=new HashMap();
        map.put("data",videoService.findById(id).getContent());
        return map;
    }

}
